﻿using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Threading;
using jQueryNET;
using System.Linq;
using System.Linq.Dynamic;
using System.Runtime.Serialization;
using System;

[DataContract]
public class RowInfo
{
	[DataMember]
	public int RowCount { get; set; }

	[DataMember]
	public Customer[] Rows { get; set; }
}

[ServiceContract(Namespace = "TreeView")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
#if DEBUG 
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
#endif
public class DataService
{
	// Add [WebGet] attribute to use HTTP GET
	[OperationContract]
	public TreeNodeCollection GetNodes(string nodeId)
	{
		var nodes = new TreeNodeCollection();

		nodes.Add(new TreeNode(nodeId + "_1", "test test test", "Images/user.png", "javascript:void(0)", "test toolTip", true, nodeId));
		nodes.Add(new TreeNode(nodeId + "_2", "test1 test test", "Images/user.png", "test.html", "test toolTip", false, nodeId));
		nodes.Add(new TreeNode(nodeId + "_3", "test2 test test", "./Images/user.png", "test.html", "test toolTip", true, nodeId));
		return nodes;
	}

	[OperationContract]
	public ComboBoxItemCollection GetListItem()
	{
		Thread.Sleep(2000);
		var result = new ComboBoxItemCollection();
		result.Add(new ComboBoxItem("test","0"));
		result.Add(new ComboBoxItem("test1", "1"));

		return result;
	}

	[OperationContract]
	public RowInfo GetRows(string fields, int first, int count, string sortField, bool sortOrder, string table)
	{
		var dataClassesContext = new DataClassesDataContext();

		IQueryable<Customer> roadQuery;

		if (string.IsNullOrEmpty(sortField))
			roadQuery = from road in dataClassesContext.Customers
						select road;
		else if (sortOrder)
			roadQuery = dataClassesContext.Customers.
				OrderBy(sortField + " ascending");
		else
			roadQuery = dataClassesContext.Customers.
				OrderBy(sortField + " descending");

		return new RowInfo() { RowCount = roadQuery.Count(), Rows = roadQuery.Skip(first).Take(count).ToArray() };
	}
	// Add more operations here and mark them with [OperationContract]
}
